package com.jxnu.duewang.zmly.zouZhen.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jxnu.duewang.zmly.BR;
import com.jxnu.duewang.zmly.R;
import com.jxnu.duewang.zmly.zouZhen.model.DatabindingDiscoverRecommendFocusData;

import java.util.List;

/**
 * Project Zmly
 * Author Duewang
 * Time 2016/12/6 0006
 */

public class DiscoverFocusImageAdapter extends PagerAdapter {

    private Context mContext;
    private List<DatabindingDiscoverRecommendFocusData> mFocusDataList;
//    private Map<Integer,View> mPool;

    public DiscoverFocusImageAdapter(Context context, List<DatabindingDiscoverRecommendFocusData> focusDataList) {
        mContext = context;
        mFocusDataList = focusDataList;
//        mPool=new Pools.SimplePool<>(focusDataList.size());
//        mPool=new ArrayMap<>();
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int location = position % mFocusDataList.size();
//        View view = mPool.get(location);
//        if (view != null) {
//            return view;
//        }else{
            DatabindingDiscoverRecommendFocusData focusData = mFocusDataList.get(location);
            View ret = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.focus_image_adapter, container, true).getRoot();
            ViewDataBinding binding = DataBindingUtil.bind(ret);
            String imageUrl = focusData.getImageUrl();
            if (imageUrl != null&&!imageUrl.isEmpty()) {
                binding.setVariable(BR.focusUrl, imageUrl);
            }else {
                ret.setBackgroundResource(R.mipmap.image_default_200);
            }
            return ret;
//        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public int getItemPosition(Object object) {
            return POSITION_NONE;
    }

}
